package num13;

import java.util.ArrayList;
import java.util.List;

public class Leecode102 {
    public class TreeNode {
     int val;
     TreeNode left;
     TreeNode right;
      TreeNode() {}
     TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }

    List<List<Integer>> list = new ArrayList<>();
    public List<List<Integer>> levelOrder(TreeNode root) {
         if (root == null) {
             return list;
         }
         sortLevel(root,0);
         return list;

    }




    public void sortLevel(TreeNode node,int level){
        if (list.size() == level){
            list.add(new ArrayList<Integer>());
        }
        list.get(level).add(node.val);
        if (node.left != null){
            sortLevel(node.left,level+1);
        }
        if (node.right != null){
            sortLevel(node.right,level+1);
        }



    }
}
